package top.humbleyuan.leet.datastructure.tree;



import java.util.Queue;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author HumbleYuan
 * @Date 2019/10/17 9:27
 * @Des
 */
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int x) {
        val = x;
    }

    static int numOfNode;   //总结点数
    /**
     * 生成一个树
     */
    public void formTree() {
        Random random = new Random();

        //队列
        Queue<TreeNode> treeNodes = new LinkedBlockingQueue<>();
        treeNodes.add(this);
        System.out.print(this.val);     // 遍历策略为层次遍历,输出格式为val(父节点，左右孩子)
        System.out.print("(root) ");

        while (!treeNodes.isEmpty() && numOfNode < 10) {
            TreeNode cur = treeNodes.poll();
            if(random.nextFloat() < 0.6) {
                cur.left = new TreeNode(random.nextInt(20) + 1);
                treeNodes.add(cur.left);
                System.out.print(cur.left.val + "(" + cur.val + ",left) ");
                numOfNode ++;
            }
            if(random.nextFloat() < 0.6) {
                cur.right = new TreeNode(random.nextInt(20) + 1);
                treeNodes.add(cur.right);
                System.out.print(cur.right.val + "(" + cur.val + ",right) ");
                numOfNode ++;
            }

        }
        System.out.println();

    }
}
